package com.dsr.service.impl;

import com.dsr.consts.PageResult;
import com.dsr.consts.Result;
import com.dsr.consts.e.BaseProperty;
import com.dsr.dao.ModelsDao;
import com.dsr.entity.Models;
import com.dsr.service.ModelsService;
import com.dsr.util.BasicUtil;
import com.dsr.util.IdWorker;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;
import java.util.stream.Collectors;

/**
 * (Models)表服务实现类
 *
 * @author dsr
 * @since 2020-09-26
 */
@Service("modelsService")
public class ModelsServiceImpl implements ModelsService {
    @Autowired
    private ModelsDao modelsDao;
    @Autowired
    private IdWorker idWorker;

    @Override
    public Result getEntity(String id) {
        Models entity = modelsDao.selectByPrimaryKey(id);
        return Result.success(entity);
    }

    @Override
    public Result save(Models models) {
        models.setId(String.valueOf(idWorker.nextId()));
        modelsDao.insertSelective(models);
        return Result.success("保存成功");
    }

    @Override
    public Result update(Models models) {
        modelsDao.updateByPrimaryKeySelective(models);
        return Result.success("修改成功");

    }

    @Override
    public Result delete(String id) {
        if (StringUtils.isNotEmpty(id)) {
                modelsDao.deleteEntity(id);
            return Result.success("删除成功");
        }
        return Result.error("请选择要删除的行");
    }
    @Override
    public Result query(int page, int size, Models models) {
        PageHelper.startPage(page, size);
        List<Models> entityList = modelsDao.select(models);
        PageInfo<Models> pageInfo = new PageInfo<>(entityList);
        return Result.success(new PageResult<Models>(pageInfo.getTotal(), pageInfo.getList()));
    }

    @Override
    public Result list() {
        List<Models> models = modelsDao.selectAll();
        return Result.success(getTree(models));
    }

    @Override
    public Result getMenu() {
        String roleId = BasicUtil.getToken(BaseProperty.ROLE_ID);
        if(StringUtils.isEmpty(roleId))
            return Result.success(new ArrayList<Models>());

        Map<String,List<Models>> listMap=new HashMap<>();
        listMap.put("opt", modelsDao.getAuthChildern(roleId));
        listMap.put("menu",modelsDao.getAuthParent(roleId));
        return Result.success(listMap);
    }

    private List<Models> getTree(List<Models> models){
        Map<String, List<Models>> collect = models.stream().filter(item -> !"0".equals(item.getParentId()))
                .collect(Collectors.groupingBy(Models::getParentId));
        List<Models> parent = models.stream().filter(item -> "0".equals(item.getParentId()))
                .collect(Collectors.toList());
        parent.forEach(item->{
            if(collect.containsKey(item.getId()))
                item.setChildren(collect.get(item.getId()));
        });
        return parent;
    }
}